package edu.seu.mart.service.impl;

import edu.seu.mart.dao.IOrderDao;
import edu.seu.mart.entity.Pager;
import edu.seu.mart.model.MartException;
import edu.seu.mart.model.Order;
import edu.seu.mart.service.IOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Administrator on 2015/3/11.
 */
@Service
public class OrderService implements IOrderService {

    @Resource
    private IOrderDao orderDao;

    @Override
    public void add(Order order) {
        orderDao.add(order);
    }

    @Override
    public void delete(int id) {
        orderDao.delete(id);
    }

    @Override
    public void update(int id, String state, Double total) {
        Order order = orderDao.load(id);
        if (order == null) {
            throw new MartException("修改状态的订单不存在");
        } else {
            order.setState(state);
            order.setTotal(total);
            orderDao.update(order);
        }
    }

    @Override
    public Pager<Order> findOrder() {
        return orderDao.findOrder();
    }

    @Override
    public void deleteChecked(String[] ids) {
        try {
            for (String id : ids) {
                orderDao.delete(Integer.parseInt(id));
            }
        } catch (Exception e) {
            throw new MartException("选中的用户删除失败");
        }
    }

    @Override
    public Pager<Order> findOrder(int orderId) {
        return orderDao.findOrderById(orderId);
    }

    @Override
    public Order load(int orderId) {
        return orderDao.load(orderId);
    }

    @Override
    public void upadteState(int id, String state) {
        Order order = orderDao.load(id);
        if (order != null) {
            order.setState(state);
            orderDao.update(order);
        }
    }

    @Override
    public Double getMoneyOfOneDayBySeller(Date time, int sellerId) {

            return orderDao.getMoneyOfOneDayBySeller(time, sellerId);
    }

    @Override
    public Double getMoneyOfOneMonthBySeller(int year, int month, int sellerId) {
        return orderDao.getMoneyOfOneMonthBySeller(year,month,sellerId);
    }

    @Override
    public Pager<Order> loadBySellerId(int id) {
        return orderDao.loadBySellerId(id);
    }

    @Override
    public Pager<Order> findBySellState(int seller, String state) {
        return orderDao.findBySellState(seller,state);
    }

    @Override
    public void updateState(int id, String state) {
        Order order = orderDao.load(id);
        if (order != null) {
            order.setState(state);
            orderDao.update(order);
        }
    }

    @Override
    public Pager<Order> loadByUserId(int id) {
        return orderDao.loadByUserId(id);
    }

    @Override
    public Pager<Order> findByUserState(int user, String state) {
        return orderDao.findByUserState(user,state);
    }

    @Override
    public Pager<Order> findByState(int seller, String s1, String s2) {
        return orderDao.findByState(seller,s1,s2);
    }
}
